/**
 * @Company:中享思途   
 * @Title:AdminLonginIntercepter.java 
 * @Author:IL Clown   
 * @Date:2020年8月25日 上午9:31:07     
 */
package com.situ.shopsystem.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/** 
 * @ClassName:AdminLonginIntercepter 
 * @Description:(这里用一句话描述这个类的作用)  
 */
public class ManagerLonginIntercepter extends HandlerInterceptorAdapter {

	/** 
	 * @Title: preHandle 
	 * @Description:(这里用一句话描述这个方法的作用)
	 * @param request
	 * @param response
	 * @param handler
	 * @return
	 * @throws Exception  
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		//得到session对象
		HttpSession session = request.getSession();
		//尝试从session中取出登录的时候放入的用户实例
		Object object = session.getAttribute("user");
		if (object != null) {//如果用户的实列存在，则判断为登录状态
			return super.preHandle(request, response, handler);
		} else {

			response.sendRedirect(getBasePath(request) + "shop/goLogin");

			return false;
		}
	}

	private String getBasePath(HttpServletRequest request) {
		StringBuffer baseBuffer = new StringBuffer();
		baseBuffer.append(request.getScheme()).append("://").append(request.getServerName()).append(":").append(request.getServerPort()).append(request.getContextPath()).append("/");
		return baseBuffer.toString();
	}
}
